<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :okAuth="'device:management:inMeasuringEquipment:view'"
    @register="registerFormDetailDrawer"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="m-1 pr-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <BasicTable @register="registerTable" >
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 设备详情 </span>
      </template>
    </BasicTable>
  </BasicDrawer>
</template>
<script lang="ts" setup name="DetailDeviceManagementInMeasuringEquipmentForm">
  import { ref, unref, computed, watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { InMeasuringEquipment, getDeviceDetails, inMeasuringEquipmentSave, inMeasuringEquipmentForm, inMeasuringEquipmentListData } from '/@/api/device.management/inMeasuringEquipment';
  import { Office, officeTreeData, officeListData, officeTreeDataList } from '/@/api/sys/office';
  import { BasicTable, BasicColumn, useTable, TableRowSelection } from '/@/components/Table';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('device.management.inMeasuringEquipment.view');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const record = ref<Recordable>({});

  const getTitle = computed(() => ({
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: t('前端采集设备'),
  }));
  const EquipmentType = ref('');

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('设备名称'),
      field: 'equipmentName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('企业名称'),
      field: 'enterpriseName',
      fieldLabel: 'enterpriseName',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('采集仪编号'),
      field: 'imei',
      fieldLabel: 'imei',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('设备类型'),
      field: 'equipmentType',
      fieldLabel: 'equipmentType',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t('安装时间'),
      field: 'createTime',
      fieldLabel: 'createTime',
      component: 'Input',
      componentProps: {
        maxlength: 255,
        disabled: true,
      },
    },
    {
      label: t(''),
      field: '',
      component: 'Divider',
      componentProps: {
        orientation: 'right',
        style: 'height:2px;background-color:black;font-size:20px;margin-top:20px;',
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const formatTime = (timestamp: number) => {
    const date = new Date(timestamp);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');
    return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('时间'),
    //   dataIndex: 'string',
    //   key: 'a.string',
    //   //sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    // 室外消火栓采集终端
    {
      title: t('状态'),
      dataIndex: 'status',
      ifShow: () => {
        return EquipmentType.value === '室外消火栓采集终端' ;
      },
      key: 'a.status',
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('压力'),
      dataIndex: 'waterPressure',
      key: 'a.waterPressure',
      ifShow: () => {
        return EquipmentType.value === '室外消火栓采集终端';
      },
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('信号(dB)'),
      dataIndex: 'signalStrength',
      key: 'a.signalStrength',
      //sorter: true,
      width: 130,
      align: 'left',
      ifShow: () => {
        return EquipmentType.value === '室外消火栓采集终端';
      },
    },
    {
      title: t('电量(%)'),
      dataIndex: 'batteryLevel',
      key: 'a.batteryLevel',
      //sorter: true,
      width: 130,
      align: 'left',
      ifShow: () => {
        return EquipmentType.value === '室外消火栓采集终端';
      },
    },
    // 无线数显液位表
    {
      title: t('液位(m)'),
      dataIndex: 'liquidLevel',
      key: 'a.liquidLevel',
      ifShow: () => {
        return EquipmentType.value === '无线数显液位表';
      },
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      ifShow: () => {
        return EquipmentType.value === '无线数显液位表' ;
      },
      key: 'a.status',
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('电量(%)'),
      dataIndex: 'batteryLevel',
      key: 'a.batteryLevel',
      ifShow: () => {
        return EquipmentType.value === '无线数显液位表';
      },
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('信号(dB)'),
      dataIndex: 'signalStrength',
      key: 'a.signalStrength',
      //sorter: true,
      width: 130,
      align: 'left',
      ifShow: () => {
        return EquipmentType.value === '无线数显液位表';
      },
    },
    
    // 无线数显压力表
    {
      title: t('水压(Mpa)'),
      dataIndex: 'waterPressure',
      key: 'a.waterPressure',
      ifShow: () => {
        return EquipmentType.value === '无线数显压力表';
      },
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      ifShow: () => {
        return EquipmentType.value === '无线数显压力表' ;
      },
      key: 'a.status',
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('电量(%)'),
      dataIndex: 'batteryLevel',
      key: 'a.batteryLevel',
      ifShow: () => {
        return EquipmentType.value === '无线数显压力表';
      },
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('信号(dB)'),
      dataIndex: 'signalStrength',
      key: 'a.signalStrength',
      //sorter: true,
      width: 130,
      align: 'left',
      ifShow: () => {
        return EquipmentType.value === '无线数显压力表';
      },
    },
    // 风速采集仪
    {
      title: t('风速'),
      dataIndex: 'windSpeed',
      key: 'a.windSpeed',
      ifShow: () => {
        return EquipmentType.value === '风速采集仪';
      },
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('风量'),
      dataIndex: 'airVolume',
      key: 'a.airVolume',
      ifShow: () => {
        return EquipmentType.value === '风速采集仪';
      },
      //sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('上报时间'),
      dataIndex: 'reportingTime',
      key: 'a.reportingTime',
      //sorter: true,
      width: 130,
      align: 'left',
      customRender: ({ record }) => {
        return formatTime(record.reportingTime);
      },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { md: 24, lg: 12 },
  });

  const [registerFormDetailDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await inMeasuringEquipmentForm(data);
    record.value = (res.inMeasuringEquipment || {}) as InMeasuringEquipment;
    record.value.__t = new Date().getTime();
    // console.log('res.InMeasuringEquipment.equipmentType', res.inMeasuringEquipment.equipmentType);
    EquipmentType.value = res.inMeasuringEquipment.equipmentType;
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  const [registerTable, { reload }] = useTable({
    api: async (params) => {
      params.value = record.value.value;
      params.deviceId = record.value.id;
      params.deviceType = EquipmentType.value;
      const response = await getDeviceDetails(params);
      // 转换数据格式
      return {
        count: response.data.count,
        list: response.data.list,
        pageNo: response.data.pageNo,
        pageSize: response.data.pageSize,
      };
    },
    immediate: false,
    columns: tableColumns,
    showTableSetting: false,
    // useSearchForm: true,
    canResize: true,
    resizeHeightOffset: 60,
    pagination: {
      defaultCurrent: 1,
      defaultPageSize: 10,
      showSizeChanger: true,
      onChange: (pageNo: number, pageSize: number) => {
        reload({
          ...record.value,
          pageNo,
          pageSize,
        });
      },
      onShowSizeChange: (pageNo: number, pageSize: number) => {
        reload({
          ...record.value,
          pageNo,
          pageSize,
        });
      },
    },
  });

  watch(() => record.value.id, () => {
    reload();
  });

  function handleSuccess(record: Recordable) {
    reload(record);
  }
</script>